class NhanViensController < ApplicationController
  # GET /nhan_viens
  # GET /nhan_viens.json
  def index
    @per_page = Param.get_value("itemsperpage")
    @search = NhanVien.where('TenNV LIKE ? OR ChucVu LIKE ? OR MaNV LIKE ?', "%#{params[:search]}%", "%#{params[:search]}%", "%#{params[:search]}%")
    @nhan_viens = @search.paginate(:page => params[:page], :per_page => @per_page)

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @nhan_viens }
      format.csv { send_data NhanVien.to_csv(@search) }
      format.xls
    end
  end

  # GET /nhan_viens/1
  # GET /nhan_viens/1.json
  def show
    @nhan_vien = NhanVien.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @nhan_vien }
    end
  end

  # GET /nhan_viens/new
  # GET /nhan_viens/new.json
  def new
    if sign_in?
      @nhan_vien = NhanVien.new

      respond_to do |format|
        format.html # new.html.erb
        format.json { render json: @nhan_vien }
      end
    else
      render 'sessions/deny'
    end
  end

  # GET /nhan_viens/1/edit
  def edit
    if sign_in?
      @nhan_vien = NhanVien.find(params[:id])
    else
      render 'sessions/deny'
    end
  end

  # POST /nhan_viens
  # POST /nhan_viens.json
  def create
    @nhan_vien = NhanVien.new(params[:nhan_vien])

    respond_to do |format|
      if @nhan_vien.save
        format.html { redirect_to @nhan_vien, notice: 'Nhan vien was successfully created.' }
        format.json { render json: @nhan_vien, status: :created, location: @nhan_vien }
      else
        format.html { render action: "new" }
        format.json { render json: @nhan_vien.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /nhan_viens/1
  # PUT /nhan_viens/1.json
  def update
    @nhan_vien = NhanVien.find(params[:id])

    respond_to do |format|
      if @nhan_vien.update_attributes(params[:nhan_vien])
        format.html { redirect_to @nhan_vien, notice: 'Nhan vien was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @nhan_vien.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /nhan_viens/1
  # DELETE /nhan_viens/1.json
  def destroy
    @nhan_vien = NhanVien.find(params[:id])
    @nhan_vien.destroy

    respond_to do |format|
      format.html { redirect_to nhan_viens_url }
      format.json { head :no_content }
    end
  end

  def import
    if sign_in?
      @ts_import = NhanVien.new
      respond_to do |format|
        format.html
      end
    else
      render 'sessions/deny'
    end
  end

  # Tạo đường dẫn để import
  def save
    if (NhanVien.import(params[:file]))
      redirect_to import_nhan_viens_path, notice: t("flash.import_msg")
    else
      redirect_to import_nhan_viens_path, notice: t("flash.import_error")
    end
  end

  def delete_user
    if params[:id] == current_user.id.to_s
      flash.now[:error] = 'You cannot delete yourself!'
      @users = NhanVien.all
      render :action => :index
    else
      NhanVien.find_by_id(params[:id]).delete
      @users = NhanVien.all
      render :action => :index
    end
  end
end
